<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    /*********************************************************
    *            --== InArch ==--
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 28.06.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/
    
    /***
    * Class logDB
    ****
    * This class is an implementation of the logProcessor interface.
    * An instance of this class will log stuff to the database
    ****
    * @package Log
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @since 28.06.2005
    * @copyright essentialmind.com 2005
    ***/
    class logDB implements logProcessor{
        
        /**
         * Reference to the database object which holds an open connection
         *
         * @var ADOConnection
         */
        private $db;
        
        /**
         * Name of the prefix to be appended to all log messages
         *
         * @var string
         */
        private $prefix;
        
        /**
         * ID of the logged in user
         *
         * @var integer
         */
        private $uid;
        
        /**
         * Whether to report the errors to the screen as well as the database
         *
         * @var bool
         */
        public $verbose;
        
        
        
        /**
         * Constructor
         *
         * @param ADOConnection $db
         */
        public function __construct(ADOConnection $db, $prefix=null,$verbose=false){
            $this->db = $db;
            $this->verbose = $verbose;
            
            if(!empty($prefix)){
                $this->prefix = '['.$prefix.']';    
            }
            
            //check if there is some sort of logged in user, as
            //the log request may very well originate from the
            //client side of the application
            if(isset($_SESSION['admin']['id']) and !empty($_SESSION['admin']['id'])){
                $this->uid = "'".$_SESSION['admin']['id']."'";   //yes, there is an admin user
            }else{
                $this->uid = "null";    //no joy, thus log all stuff to the 'www' user
            }
        }
        
        
        /**
         * Makes a record of some defined action
         *  
         * @param string  $text   - Action description
         * @param integer $code   - Action code
         * @param boolean $silent - Silent mode? (if true then any
         *                          output to the screen will be
         *                          supressed
         * @return void
         **/
        public function recordAction($text,$code=0,$silent=true){
            //see if stuff has to go to the browser as well
            if($this->verbose && !$silent){
                echo $text ."\n";   
            }
            
            /*
            $this->db->Execute("INSERT INTO log_action(message,code,ip,page,uid) 
                                VALUES('".$this->prefix." ".addslashes($text)."','".$code."','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['REQUEST_URI']."',$this->uid)");
            */
            #@mail('a.kulikov@gmail.com','INA ERROR',$this->prefix." ".addslashes($text)." ".$_SERVER['REMOTE_ADDR']." ".$_SERVER['REQUEST_URI']);
        }
        
        
        /**
         * Write error data to the database
         * 
         * @param string  $text   - Error description
         * @param integer $code   - Error code
         * @param boolean $silent - Silent mode? (if true then any
         *                          output to the screen will be
         *                          supressed) -- note that in this class
         *                          this parameter is not used at all
         * @return void
         **/
        public function recordError($text,$code=0,$silent=false){
            //see if stuff has to go to the browser as well
            if($this->verbose && !$silent){
                echo $text;   
            }
            
            /*
            $this->db->Execute("INSERT INTO log_error(message,code,ip,page,uid) 
                                VALUES('".$this->prefix." ".addslashes($text)."','".$code."','".$_SERVER['REMOTE_ADDR']."','".$_SERVER['REQUEST_URI']."',$this->uid)");
            */
            if(!$GLOBALS['_CONFIG']['print_errors'] and $code != 404){
                $message = $this->prefix." ".addslashes($text)." ".$_SERVER['REMOTE_ADDR']." ".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."\n".$_SERVER['HTTP_REFERER'];
                @mail($GLOBALS['_CONFIG']['god'], 'INA ERROR @ '.$_SERVER['HTTP_HOST'], $message);
            }
        }
    }
?>